<?php
namespace Home\Controller;
use Think\Controller;

Class UsermoneyController extends CommonController{

    private $uid = '';

    public function _initialize() {
        parent::_initialize();
        $this->dbname = CONTROLLER_NAME;
    }

    //首页
    public function index(){
         $y = date('Y-m',time());
         $data = $y;
        if(IS_POST){
            if($_POST['time1'] != ''){
                $str = substr($_POST['time1'],0,7);
                $data = $str;
            }
            $this->map($_REQUEST);
            $this->assign('uid',$_REQUEST['username']);
            $uid = $_REQUEST['username'];
        }else{
            $uid = 21;
            $this->assign('uid',21);
        }
        $details = $this->monthMoney($uid,$fd=1,$data);
        $name = M('user')->field('truename,id')->where('status = 1')->select();
        $this->assign('data',$data);
        $this->assign('details',$details);
        $this->assign('xyname',$name);
    	$this->display();
    }

    //查询条件操所
    public function map($val){
        if(is_array($val)){
            $this->uid = $val['username'];
        }else{
            $this->uid = $val;
        }   
        //var_dump($this->uid);exit; 
    }
	
	//计算单个人的月工资
	public function monthMoney($uid,$fd,$data){

		//统计月份
		$mon = $data;

        //查询的开始时间
        $strattime = strtotime($mon.'-'.'01');

        //查询的结束时间
 	    $endtime = strtotime($mon.'-'.'01 +1 month');

 	    //个人入职的时间
 	    $creat_time = M('user')->where("id = {$uid}")->field('creat_time')->find();
      
        if($creat_time['creat_time'] > $endtime){
            //var_dump('没有数据');exit;
        }else{
        	  if($creat_time['creat_time'] >= $strattime && $creat_time['creat_time'] < $endtime){
		        	$strattime = $creat_time['creat_time'];
		        }elseif($creat_time['creat_time'] < $strattime){
					$strattime = $strattime;
					$endtime = $endtime;
				}else{
		        	$strattime = $endtime; 
		        	$endtime = $creat_time['creat_time'];	
		        }

		   $result = M('cash_order')
 	                             ->join("cash_bf_dk ON cash_order.order_id = cash_bf_dk.order_id",'right')->where("{$strattime} <= cash_bf_dk.k_time AND cash_bf_dk.k_time < {$endtime} AND cash_bf_dk.collection_id = {$uid} AND cash_bf_dk.status = 'S'")->field('cash_order.order_id,cash_order.uid,cash_order.status,cash_order.loan_time,cash_order.days,cash_bf_dk.k_time,cash_bf_dk.trans_type,cash_bf_dk.over_day,cash_bf_dk.succ_amt')->select();

	 	    for($i=0;$i<count($result);$i++){
	 	    	if($result[$i]['trans_type'] == 3){
	 	    		$arr[] = $result[$i];
	 	    	}else{
	 	    		$arr1[] = $result[$i];
	 	    	}
	 	    }
//echo count($arr).'++---'.count($arr1);
            if($fd == 'type'){
                //办理延期的
                $val = $this->yanqi($arr,$uid);
            }elseif($fd == 1){
                $val['yanqi'] = $this->yanqi($arr,$uid);
                $val['huankuan'] = $this->huankuan($arr1,$uid);
            }else{
                //办理还款的
                $val = $this->huankuan($arr1,$uid);
            }
	 	    return $val;
       
        }
 	    
	}



	//办理延期的单子
	public function yanqi($arr,$uid){
		 for($j=0;$j<count($arr);$j++){
        	$days = (($arr[$j]['k_time'] - $arr[$j]['loan_time']) / 86400);
        	if($days > $arr[$j]['days']){
                 $a++;
                $arrss['order_id'] = ['eq',$arr[$j]['order_id']];
                $arrss['k_time'] = ['LT',$arr[$j]['k_time']];
                $arrss['status'] = ['eq','S'];
                $k_time = M('cash_bf_dk')->where($arrss)->order('k_time desc')->find();

                     if ($k_time['k_time']>0) {
                         $day = (($arr[$j]['k_time']-$k_time['k_time'])/86400);
                         $delay = ceil($day-$k_time['over_day']);
                     } else {
                         $day = (($arr[$j]['k_time']-$arr[$j]['loan_time'])/86400);
                         $delay = ceil($day-$arr[$j]['days']);
                     }
        
        	}else{
                $b++;
                $delay = ceil($days-$arr[$j]['days']);
        	}


             if ($delay<=0) {
                 $count['<=0']++;
             } elseif ($delay>0 && $delay<=3) {
                 $count['0<delay<=3']++;
             } elseif ($delay>3 && $delay<=7) {
                 $count['3<delay<=7']++;
             } elseif ($delay>7 && $delay<=10) {
                 $count['7<delay<=10']++;
             } else {
                $count['>10xu']++;
             }
        }
             //获取当前查询人的姓名
             $uname = $this->searchName($uid);
             $money = $count['<=0'] + $count['0<delay<=3']*2 + $count['3<delay<=7']*10 
                  + $count['7<delay<=10']*20 + $count['>10xu']*50;
             $yanqi['uname'] = $uname['truename'];
             $yanqi['type'] = '延期订单';
             $yanqi['<0'] = $count['<=0'];
             $yanqi['0-3']=$count['0<delay<=3'];
             $yanqi['3-7']=$count['3<delay<=7'];
             $yanqi['7-10']=$count['7<delay<=10'];
             $yanqi['>10']=$count['>10xu'];
             $yanqi['zong'] = array_sum($count);
             $yanqi['countmoney']=$money;
             return $yanqi;    
	}

    //获取当前查询人的姓名
    public function searchName($uid){
        $uname = M('user')->where("id = {$uid}")->field('truename')->find();
        return $uname;
    }
	//办理还款的
	public function huankuan($arr1,$uid){

        for($j=0;$j<count($arr1);$j++){
            $days = (($arr1[$j]['k_time'] - $arr1[$j]['loan_time']) / 86400);
            if($days > $arr1[$j]['days']){

                $arrss['order_id'] = ['eq',$arr1[$j]['order_id']];
                $arrss['k_time'] = ['LT',$arr1[$j]['k_time']];
                $arrss['status'] = ['eq','S'];
                $k_time = M('cash_bf_dk')->where($arrss)->order('k_time desc')->find();

                     if ($k_time['k_time']>0) {
                         $day = (($arr1[$j]['k_time']-$k_time['k_time'])/86400);
                         $delay = ceil($day-$k_time['over_day']);
                     } else {
                         $day = (($arr1[$j]['k_time']-$arr1[$j]['loan_time'])/86400);
                         $delay = ceil($day-$arr1[$j]['days']);
                     }
               
            }else{
                $delay = ceil($days-$arr1[$j]['days']);
            }


             if ($delay<=0) {
                 $count['<=0']++;
             } elseif ($delay>0 && $delay<=3) {
                 $count['0<delay<=3']++;
             } elseif ($delay>3 && $delay<=7) {
                 $count['3<delay<=7']++;
             } elseif ($delay>7 && $delay<=10) {
                 $count['7<delay<=10']++;
             } else {
                 $count['>10']++;
             }
        }
             $uname = $this->searchName($uid);
             $money = $count['<=0'] + $count['0<delay<=3']*2 + $count['3<delay<=7']*10 
                  + $count['7<delay<=10']*20 + $count['>10']*200;
            $yanqi['uname'] = $uname['truename'];$yanqi['type'] = '还款订单';$yanqi['<0'] = $count['<=0'];$yanqi['0-3']=$count['0<delay<=3'];$yanqi['3-7']=$count['3<delay<=7'];$yanqi['7-10']=$count['7<delay<=10'];$yanqi['>10']=$count['>10'];$yanqi['zong'] = array_sum($count);$yanqi['countmoney']=$money;
            return $yanqi;

	}

	//延期图形
	public function type(){
        $uid = $_GET['uid'];
        $data = $_GET['data'];
		$this->yqfenxi('type','延期统计',1,$uid,$data);
	}
   
    //还款图形
    public function back(){
        $uid = $_GET['uid'];
        $data = $_GET['data'];
        $this->yqfenxi('huankuan','还款统计',2,$uid,$data);
    }
    public function yqfenxi($fd,$ft,$type,$uid,$data) {
        import("Org.Util.Chart");
        $chart = new \Chart;
        $xiangqing = $this->monthMoney($uid,$fd,$data);
        $title = $ft; 
        $data = array($xiangqing['<0'],$xiangqing['0-3'],$xiangqing['3-7'],$xiangqing['7-10'],$xiangqing['>10']); 
        $size = 140; 
        $width = 550; 
        $height = 300; 
        $legend = array('<0','0-3','3-7','7-10','>10');
        ob_end_clean();
         if ($type == 1) {
            $chart->create3dpie($title,$data,$size,$height,$width,$legend);
         }
         if ($type == 2) {
            $chart->createcolumnar($title,$data,$size,$height,$width,$legend);
         }
         if ($type == 3) {
            $chart->createmonthline($title,$data,$size,$height,$width,$legend);
         }
         if ($type == 4) {
            $chart->createring($title,$data,$size,$height,$width,$legend);
         }
         if ($type == 5) {
            $chart->createhorizoncolumnar($title,$subtitle,$data,$size,$height,$width,$legend);
         }
   
    }

    //导出当前人的数据表
    public function outexcel(){
        //获取要查询人员的id
        $where['posname'] = '风控审核专员';
        $where['status'] = 1;
        $arr = M('user')->where($where)->field('id')->select();
       // $arr = array(21,22,24,26,30);
        foreach($arr as $k=>$v){
            $val[] = $this->monthMoney($v['id'],$fd=1,$_REQUEST['time']);
        }
        $valArr = array('姓名','类型','小于0天','0-3天','3-7天','7-10天','大于10天','总单数','总钱数');
        $filename = $_REQUEST['time'].'月催收详情';      
        $this->xlsout($filename,$valArr,$val);
    }
}